<?php
class Func_Mail{
	
	//TODO: these should be user defined in the config file
	const SMTP = "smtp.googlemail.com";
	const SENDMAIL_FROM = "donotreply@poll-vault.net";
	const SMTP_PORT = 465;
	const SENDMAIL_PATH = "/usr/sbin/sendmail -t -i";
	
	private function configure(){

		ini_set("SMTP", self::SMTP);
    	ini_set("sendmail_from", self::SENDMAIL_FROM);
		ini_set("smtp_port", self::SMTP_PORT);
		ini_set("sendmail_path", self::SENDMAIL_PATH);
				
		// define a mail log for debugging
		ini_set("mail_log", "/var/log/mail.log");
	}
	
	/**
	 * Send an email using the sendmail configuration defined in this class.
	 * Defining the type paramater as 'html' will send an html email.
	 * Default content type is plain text.
	 *  
	 * @param $to
	 * @param $subject
	 * @param $body
	 * @param $type
	 */
	public static function send($to, $subject, $body, $type = null){

		Validate::notNull($to, "[Func_Mail]: email to address is null");
		
		switch($type){
			case "html":
				$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
				break;
			default:
				$headers = 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";
				break;
		}
		
		//TODO: verify email address is valid
		
		mail($to, $subject, $body, $headers);
	}
	
	/**
	 * gets an html email template from the html template directory
	 * 
	 * @param $location
	 * @param array $placeholderValues(array('placeholder', value))
	 */
	public static function getEmailHTMLTemplate($location, array $placeholderValues){
		$smarty = new Smarty();
		
		for($i=0; $i<count($placeholderValues); $i++){
			$pv = $placeholderValues[$i];
			$smarty->assign($pv['placeholder'], $pv['value']);
		}
		
		return $smarty->fetch($location);
	}
}
?>
